package com.probiz.estore.system.service.impl;

import java.io.Serializable;
import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.AppMenu;
import com.probiz.estore.common.model.system.AppResource;
import com.probiz.estore.common.model.system.AppRole;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.system.dao.AppRoleDao;
import com.probiz.estore.system.service.AppRoleManager;


/**
 * Manager implementation for AppRole, responsible for business processing, and communicate between web and persistence layer.
 */
public class AppRoleManagerImpl extends GenericManagerImpl<AppRole> implements AppRoleManager {

	private AppRoleDao appRoleDao = null;

	/**
	 * @param appRoleDao
	 *            the appRoleDao to set
	 */
	public void setAppRoleDao(AppRoleDao appRoleDao) {
		this.appRoleDao = appRoleDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = appRoleDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(AppRole entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(AppRole entity) {
	
	}
	
	/**
	 * get all roles which id not in roleIds.
	 * @param roleIds
	 * @return List
	 */
	public List getRolesNotInIds(String roleIds) {
        return appRoleDao.getRolesNotInIds(roleIds);
    }
	
	public boolean getIsRoleNameExist(String roleName) {
		return appRoleDao.getIsRoleNameExist(roleName);
	}
	

	public AppRole getAdminRole() {
		return appRoleDao.getByRoleName(Constants.DEFAULT_ADMIN_ROLE_NAME);
	}
	
	@Override
	protected void delete(AppRole entity) {
		preDelete(entity);
		if(Constants.DEFAULT_ADMIN_ROLE_NAME.equals(entity.getRoleName()))
			return;
		this.dao.delete(entity);
		
	}
	
	@Override
	public void save(AppRole entity) {
		if(!Constants.DEFAULT_ADMIN_ROLE_NAME.equals(entity.getRoleName())){
			entity.getAppResources().clear();
			if(entity.getResourcesTemp()!=null)
				for(AppResource rs: entity.getResourcesTemp()){
					entity.getAppResources().add(rs);
				}
			entity.getAppMenus().clear();
			if(entity.getMenusTemp()!=null)
				for(AppMenu rs: entity.getMenusTemp()){
					entity.getAppMenus().add(rs);
					//rs.getAppRoles().add(entity);
				}
		}
		dao.save(entity);
	}
	

	public List<AppRole> findAppRoles(Short belongTo){
		return dao.findByHql("from AppRole where belongTo=? order by roleId asc", belongTo);
	}

	public List<AppRole> getCustomerRoles() {
		return dao.findByHql("from AppRole where belongTo = 1 order by roleId asc");
	}
	
	public List<AppRole> getSystemRoles() {
		return dao.findByHql("from AppRole where belongTo = 0 order by roleId asc");
	}

	public AppRole getRoleByName(String roleName) {
		return appRoleDao.getByRoleName(roleName);
	}
	
	public List<AppRole> listRoleTree(Short belongTo, Serializable exceptRoleId) {
		return this.appRoleDao.listRoleTree(belongTo);
	}
}
